<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class BookModel extends Model
{
    protected $table = 'book';
    public  $timestamps = false;

    /**
     * @param $bookname
     * @return mixed
     * 获取图书，模糊查询或查询所有
     */
    public function getBook($bookname)
    {
        return $this->where('bookname','like',$bookname)->get();
    }

    /**
     * @param $bookid
     * @return bool
     * 删除图书
     */
    public function deleteBookById($bookid)
    {
        try{
            $this->where('bookid',$bookid)->delete();
        } catch (\PDOException $e) {
            $e->getMessage();
            return false;
        }
        return true;
    }

    /**
     * @param $bookid
     * @param int $status
     * @return bool
     * 借书或者还书
     */
    public function borrowBook($bookid, $status = 0)
    {
        try {
            $this->where('bookid', $bookid)->update(['status' => $status]);
        } catch (\PDOException $e) {
            $e->getMessage();
            return false;
        }
        return true;
    }

    //添加图书
    public function addBook(array $BookInfo)
    {
        $BookInfo['status'] = 1;
        try {
            $this->insert($BookInfo);
        } catch (\PDOException $e) {
            $e->getMessage();
            return false;
        }
        return true;
    }

    //修改图书信息
    public function updateBook(array $BookInfo, $bookid)
    {
        try {
            $this->where('bookid', $bookid)->update($BookInfo);
        } catch (\PDOException $e) {
            $e->getMessage();
            return false;
        }
        return true;
    }
}
